
/*
 * Copyright (c) 2013.
 * 游戏服务器核心代码编写人陈磊拥有使用权
 * 联系方式：E-mail:13638363871@163.com ;qq:502959937
 * 个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver.serverCore.controller.handleController.pvpCoreController;

import com.dc.gameserver.hardCode.PkStatus;
import com.dc.gameserver.proto.gameserverprotoVo.keepBattleRequest;
import com.dc.gameserver.proto.gameserverprotoVo.keepBattleResponse;
import com.dc.gameserver.proto.gameserverprotoVo.netCardVO;
import com.dc.gameserver.proto.gameserverprotoVo.readyOkResponse;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.service.cardModule.Card;
import com.dc.gameserver.serverCore.service.cardModule.CardBox;
import com.dc.gameserver.serverCore.service.cardModule.CardBoxManager;
import com.dc.gameserver.serverCore.service.character.GameCharacter;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import javolution.util.FastTable;
import org.springframework.stereotype.Service;

/**
 * @author : 石头哥哥
 *         Project : LandlordsServer
 *         Date: 13-6-4
 *         Time: 上午11:49
 *         Connect: 13638363871@163.com
 *         packageName: Server.ServerCore.Controller
 *         keep on  PK
 *         this is a loop point!
 */
@Service
public class keepBattleReadyController extends AbstractController {

    /***ID**/

    /**
     */
    @Override
    public void PostConstruct() {
        int ID=keepBattleRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]= keepBattleRequest.getDefaultInstance();
    }

     /**
     * messageLite数据结构体分发
     *
     * @param messageLite
     * @param player
     * @throws Exception
      *
      * 匹配玩家，初始化出牌数据
     */
    @Override
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        /**
         *  this  is loop point .
         *  if av8d ready for pk,and then server will out card for players
         */
        keepBattleRequest keepBattlePKRequest= (keepBattleRequest) messageLite;
        boolean readyStatus=keepBattlePKRequest.getReady();
        keepBattlePKRequest=null;
        keepBattleResponse.Builder builder= keepBattleResponse.newBuilder();
        builder.setReady(readyStatus);
        builder.setPlayerID(player.getID());
        CardBox cardBox= CardBoxManager.getCardBoxByID(player.getCardBoxID());
        cardBox.submitMessageToEveryOne(wrappedBufferShort(keepBattleResponse.msgID.ID_VALUE, builder.build()));

        player.setPkStatus(PkStatus.YES_PK);
        FastTable<GameCharacter> checkList=cardBox.getCheckOutCardPlayerList();
        checkList.add(player);

         FastTable<GameCharacter> playerInstances=cardBox.getInGamePlayerList();
         if (cardBox.checkKeepBattleReady()&&checkList.size()==2){
             GameCharacter  otherPlayer=cardBox.getOtherPlayer(player);
             if (player.isFirstHand()){
                 cardBox.ClearAndIntiCardBox(player, otherPlayer);
             }  else {
                 //  otherPlayer player 先手
                 cardBox.ClearAndIntiCardBox(otherPlayer, player);
             }

             //send card list
             for (GameCharacter p:playerInstances){
                 sendList(p);
             }

             /***
              * {@link battleReadyController#DispatchMessageLit(com.google.protobuf.MessageLite, com.dc.gameserver.serverCore.model.player.PlayerInstance)}
              * {@link CardBoxManager#MatchCardBox(com.dc.gameserver.serverCore.model.player.PlayerInstance, com.dc.gameserver.serverCore.model.player.PlayerInstance)}
              * **/
             int round=cardBox.RoundCount();
             CardBoxManager.notifyOutCards(player,true,round);
            CardBoxManager.notifyOutCards(otherPlayer,true,round);
         }

    }

    /**
     * 推送指定玩家的牌数据列表
     * @param player
     */
    private  void sendList(GameCharacter player){
        readyOkResponse.Builder builder= readyOkResponse.newBuilder();
        FastTable<Card> PlayerCardList=player.getCardlib();
        for (com.dc.gameserver.serverCore.service.cardModule.Card card:PlayerCardList){
           netCardVO.Builder netCardBuilder=netCardVO.newBuilder();
            netCardBuilder.setCardNum(card.getType());//编号
            netCardBuilder.setCardCount(card.getCount());//点数
            builder.setCardList(netCardBuilder);
        }
        player.DirectSend(wrappedBufferShort(readyOkResponse.msgID.ID_VALUE,builder.build()));
    }
}
